import {
	baseUrl
} from '@/config/config.js'
/**
 * 发送请求
 */
function baseRequest(url, method, data, ) {
	let Url = baseUrl

	return new Promise((reslove, reject) => {
		uni.request({
			url: Url + '/api/' + url,
			method: method || 'GET',
			header: {
				'Content-Type':'application/x-www-form-urlencoded'
			},
			data: data || {},
			success: (res) => {
				if (res.data.status == 1)
					reslove(res.data, res);
				else {
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					})
					reject(res.data.msg || '系统错误');
				}


			},
			fail: (msg) => {
				let data = {
					mag: '请求失败',
					status: 1 //1没网
				}
				// #ifdef APP-PLUS
				reject(data);
				// #endif
				// #ifndef APP-PLUS
				reject('请求失败');
				// #endif
			}
		})
	});
}

const request = {};

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (api, data, opt) => baseRequest(api, method, data, opt || {})
});



export default request;